package com.nanyang.tms.controller.waybill;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.tms.api.dto.request.waybill.CalWeightRequest;
import com.nanyang.tms.api.dto.request.waybill.WaybillAddReuqest;
import com.nanyang.tms.api.dto.request.waybill.WaybillShipmentAddRequest;
import com.nanyang.tms.api.dto.response.forecast.ForecastDetailResponse;
import com.nanyang.tms.api.dto.response.waybill.WeightResponse;
import com.nanyang.tms.api.service.forecast.ForecastCoreRpcService;
import com.nanyang.tms.api.service.waybill.WaybillCoreRpcService;
import com.nanyang.tms.api.service.weight.CalWeightRpcService;

@RestController
@RequestMapping("/waybill")
public class WaybillCoreController {
     @Reference(version = "1.0.0", timeout = 10000, check = false)
     private CalWeightRpcService calWeightRpcService;
     @Reference(version = "1.0.0", timeout = 10000, check = false)
     private ForecastCoreRpcService forecastCoreRpcService;
     @Reference(version = "1.0.0", timeout = 10000, check = false)
     private WaybillCoreRpcService waybillCoreRpcService;
    
    @RequestMapping(value = "/forcast/info/{ticketNo}", method = RequestMethod.GET)
    public ApiResult<ForecastDetailResponse> getForecastDetail(
    		@PathVariable("ticketNo") 
    		@NotNull(message = "服务商单号不能为空") 
			@Size(max = 32, message = "服务商单号最大长度是32个字符")
    		String ticketNo){
    	 ApiResult<ForecastDetailResponse> apiResult = forecastCoreRpcService.getForecastDetail(ticketNo);
    	 if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
             throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
         }
         return apiResult;
    }
    
    @RequestMapping(value = "/calweight", method = RequestMethod.POST)
    public ApiResult<WeightResponse> calWeight(@RequestBody @Valid  CalWeightRequest calWeightRequest){
    	ApiResult<WeightResponse> apiResult = calWeightRpcService.calWeightDetail(calWeightRequest);
    	 if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
             throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
         }
		return apiResult;
    	
    }
    @BindParam
    @RequestMapping(value = "/receipt", method = RequestMethod.POST)
    public ApiResult<Boolean> receipt(@RequestBody @Valid @BindParam WaybillAddReuqest waybillAddReuqest){
    	ApiResult<Boolean> apiResult = waybillCoreRpcService.saveWaybill(waybillAddReuqest);
    	 if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
             throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
         }
    	return apiResult;
    	
    }
    
    @RequestMapping(value = "/receipt/info/{ticketNo}", method = RequestMethod.GET)
    public ApiResult<String> getReceiptInfo(@PathVariable("ticketNo") @NotNull(message = "服务商单号不能为空") String ticketNo ){
    	ApiResult<String> apiResult = waybillCoreRpcService.getCompanyNameBySupplierNo(ticketNo);
    	 if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
             throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
         }
    	return apiResult;
    }
    @BindParam
    @RequestMapping(value = "/shipment", method = RequestMethod.POST)
    public ApiResult<Boolean> shipmentt(@RequestBody @Valid  @BindParam WaybillShipmentAddRequest waybillShipmentAddRequest){
    	ApiResult<Boolean> apiResult = waybillCoreRpcService.saveWaybillShipment(waybillShipmentAddRequest);
    	 if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
             throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
         }
    	return apiResult;
    }

    
}
